/*
 * @brief Sensor Demo serial I/O command (header file)
 *
 * @note
 * Copyright(C) NXP Semiconductors, 2015
 * All rights reserved.
 *
 * @par
 * Software that is described herein is for illustrative purposes only
 * which provides customers with programming information regarding the
 * LPC products.  This software is supplied "AS IS" without any warranties of
 * any kind, and NXP Semiconductors and its licensor disclaim any and
 * all warranties, express or implied, including all implied warranties of
 * merchantability, fitness for a particular purpose and non-infringement of
 * intellectual property rights.  NXP Semiconductors assumes no responsibility
 * or liability for the use of the software, conveys no license or rights under any
 * patent, copyright, mask work right, or any other intellectual property rights in
 * or to any products. NXP Semiconductors reserves the right to make changes
 * in the software without notification. NXP Semiconductors also makes no
 * representation or warranty that such application will be suitable for the
 * specified use without further testing or modification.
 *
 * @par
 * Permission to use, copy, modify, and distribute this software and its
 * documentation is hereby granted, under NXP Semiconductors' and its
 * licensor's relevant copyrights in the software, without fee, provided that it
 * is used in conjunction with NXP Semiconductors microcontrollers.  This
 * copyright, permission, and disclaimer notice must appear in all copies of
 * this code.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#include "lpcusbsio.h"
#include "i2c_io.h"
#include "trace.h"

#include "sio_cmd.h"

static void cmd_whoami(char* cmd);
static void cmd_get_version(char* cmd);
static void cmd_reset(char* cmd);
static void cmd_get_data_len(char* cmd);
static void cmd_get_data(char* cmd);

typedef void (*CMD_FN_T)(char* cmd);

typedef struct {
	char*		str;
	CMD_FN_T	fn;
} CMD_PROC_T;

static CMD_PROC_T c_tab[] = {
	{	"who am i",		cmd_whoami				},
	{	"whoami",		cmd_whoami				},
	{	"ver",			cmd_get_version			},
	{	"version",		cmd_get_version			},
	{	"reset",		cmd_reset				},
	{	"length",		cmd_get_data_len		},
	{	"data",			cmd_get_data			},
};
static const uint32_t c_tab_len = sizeof(c_tab) / sizeof(CMD_PROC_T);
	
void sio_command(char* cmd)
{
	uint32_t i;

	trace_enter();
	printf("Command: %s\n", cmd);
	for (i=0; i<c_tab_len; i++)
		if (strcmp(cmd, c_tab[i].str) == 0)
			(*c_tab[i].fn)(cmd);
	trace_exit();
}


static void cmd_whoami(char* cmd)
{
	uint8_t wai = 0;

	trace_enter();
	i2c_sd_init();
	if (LPCUSBSIO_GetNumPorts(LPCUSBSIO_VID, LPCUSBSIO_PID) > 0) {
		i2c_sd_open();
		wai = i2c_sd_who_am_i();
		printf("Who am I: 0x%02x\n", wai);
		i2c_sd_close();
	} else {
		printf("No sio ports available\n");
	}
	trace_exit();
}

static void cmd_get_version(char* cmd)
{
	uint16_t ver=0;

	trace_enter();
	i2c_sd_init();
	if (LPCUSBSIO_GetNumPorts(LPCUSBSIO_VID, LPCUSBSIO_PID) > 0) {
		i2c_sd_open();
		ver = i2c_sd_get_version();
		printf("Firmware Version: 0x%04x\n", ver);
		i2c_sd_close();
	} else {
		printf("No sio ports available\n");
	}
	trace_exit();
}

static void cmd_reset(char* cmd)
{
	trace_enter();
	printf("Reset command not yet implemented.\n");
	trace_exit();
}

static void cmd_get_data_len(char* cmd)
{
	trace_enter();
	printf("Get data length command not yet implemented.\n");
	trace_exit();
}

static void cmd_get_data(char* cmd)
{
	trace_enter();
	printf("Get data command not yet implemented.\n");
	trace_exit();
}
